package com.example.experiment_six;


import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.SocketTimeoutException;


public class MainActivity extends AppCompatActivity {
    EditText input;
    TextView show;
    Button send;
    Handler handler;
    ClientThread clientThread;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        input = (EditText)findViewById(R.id.input);
        show = (TextView)findViewById(R.id.show);
        send = (Button)findViewById(R.id.send);
        handler = new Handler(){
            @Override
            public void handleMessage(Message msg){
                if(msg.what==0x123){
                    show.setText(show.getText().toString()+msg.getData().getString("receive_content"));
                }
            }
        };
        clientThread = new ClientThread(handler);
        new Thread(clientThread).start();

        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try{
                    Message msg = new Message();
                    msg.what = 0x345;
                    msg.obj = input.getText().toString();
                    clientThread.revHandler.sendMessage(msg);
                    input.setText("");
                }catch (Exception e){
                    e.printStackTrace();
                }

            }
        });
    }
}
class ClientThread implements Runnable{
    private Socket s;
    private Handler handler;
    public Handler revHandler;
    BufferedReader br = null;
    OutputStream os =null;
    public ClientThread(Handler handler){
        this.handler=handler;
    }
    public void run(){
        try{
            s = new Socket("127.0.0.1",30000);
            br = new BufferedReader(new InputStreamReader(s.getInputStream()));
            os = s.getOutputStream();
            new Thread(){
                @Override
                public void run(){
                    String content = null;
                    try{
                        while ((content = br.readLine()) != null){
                            Message msg = new Message();
                            msg.what = 0x123;
                            Bundle bundle = new Bundle();
                            bundle.putString("receive_content",content);
                            msg.setData(bundle);
                            //msg.obj = content;
                            handler.sendMessage(msg);
                        }
                    }
                    catch (IOException e){
                        e.printStackTrace();
                    }
                }
            }.start();
            Looper.prepare();
            revHandler = new Handler() {
                @Override
                public void handleMessage(Message msg){
                    if(msg.what==0x345){
                        try{
                            os.write((msg.obj.toString() + "\r\n").getBytes("utf-8"));
                        }
                        catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                }
            };
            Looper.loop();
        }
        catch (SocketTimeoutException e1){
            System.out.println("网络连接超时！！");
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
}